//
//  DLQQShareManager.m
//  DGGSharePayKit
//
//  Created by 伍明鹏 on 2020/4/13.
//

#import "DLQQShareManager.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

@interface DLQQShareManager()<QQApiInterfaceDelegate,TencentSessionDelegate>

@end
@implementation DLQQShareManager

- (NSString *)getBundleID{
    NSString *key = (NSString *)kCFBundleIdentifierKey;
    return [NSBundle mainBundle].infoDictionary[key];
}

- (NSString *)getAppName{
    return [NSBundle mainBundle].infoDictionary[@"CFBundleDisplayName"];
}

+ (instancetype)sharedInstance{
    static DLQQShareManager *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[DLQQShareManager alloc] init];
        [_sharedInstance getAppName];
        [_sharedInstance getAppName];
        NSLog(@"BUNDLEID:%@,APPNAME:%@",[_sharedInstance getAppName],[_sharedInstance getAppName]);
    });
    return _sharedInstance;
}

#pragma mark - QQ授权
- (void)dl_qqAuthLoginWithappId:(NSString *)appId universalLink:(NSString *)universalLink{
     NSArray *permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO, kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];
    // 这里调起登录
    TencentOAuth *tencentOAuth = [[TencentOAuth alloc] initWithAppId:appId andDelegate:self];
    [tencentOAuth authorize:permissions];
}

/**
 * 登录成功后的回调
 */
- (void)tencentDidLogin{
    
}

/**
 * 登录失败后的回调
 * \param cancelled 代表用户是否主动退出登录
 */
- (void)tencentDidNotLogin:(BOOL)cancelled{
    
}

// 获取用户信息
- (void)getUserInfoResponse:(APIResponse *)response {
    
    if (response && response.retCode == URLREQUEST_SUCCEED) {
        
        NSDictionary *userInfo = [response jsonResponse];
        NSString *nickName = userInfo[@"nickname"];
        
        // 后续操作...
        
        
    } else {
        NSLog(@"QQ auth fail ,getUserInfoResponse:%d", response.detailRetCode);
    }
}
//
- (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth
withPermissions:(NSArray *)permissions {
     // incrAuthWithPermissions是增量授权时需要调用的登录接口
    // permissions是需要增量授权的权限列表
    [tencentOAuth incrAuthWithPermissions:permissions];
    return NO; // 返回NO表明不需要再回传未授权API接口的原始请求结果；
    // 否则可以返回YES
}

#pragma mark - QQ分享
- (void)dl_shareWithMediaURL:(NSString *)url
          description:(NSString *)description
          title:(NSString *)title
previewImageURL:(NSURL *)previewImageURL{
    
    QQApiNewsObject *newsObj = [QQApiNewsObject
                                objectWithURL:[NSURL URLWithString:url]
                                title:title
                                description:description
                                previewImageURL:previewImageURL];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    NSLog(@"QQ内容分享%@",req);
    if ([self.delegate respondsToSelector:@selector(dl_sharedMessageCallBack:responseData:)]) {
        if (sent != EQQAPISENDSUCESS) {
            [self.delegate dl_sharedMessageCallBack:NO responseData:nil];
        }
        
    }
}

- (void)dl_shareWithMediaURL:(NSString *)url
          description:(NSString *)description
          title:(NSString *)title
            previewImageData:(NSData *)previewImageData{
    QQApiNewsObject *newsObj = [QQApiNewsObject
                                objectWithURL:[NSURL URLWithString:url]
                                title:title
                                description:description
                                previewImageData:previewImageData];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    NSLog(@"QQ内容分享%@",req);
    if ([self.delegate respondsToSelector:@selector(dl_sharedMessageCallBack:responseData:)]) {
        if (sent != EQQAPISENDSUCESS) {
            [self.delegate dl_sharedMessageCallBack:NO responseData:nil];
        }
    }
}

- (void)dl_shareImageWithData:(NSData*)data
{
    QQApiImageObject *object = [[QQApiImageObject alloc] initWithData:data previewImageData:nil title:nil description:nil];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    if ([self.delegate respondsToSelector:@selector(dl_sharedMessageCallBack:responseData:)]) {
        if (sent != EQQAPISENDSUCESS) {
            [self.delegate dl_sharedMessageCallBack:NO responseData:nil];
        }
    }
}
    
#pragma mark - 回调处理
- (BOOL)dl_qqHandleOpenURL:(NSURL *)theURL{
    return [QQApiInterface handleOpenURL:theURL delegate:self];
}

/**
handle open url
*/
- (BOOL)dl_handleOpenURL:(NSURL *)theURL{
    return [self dl_handleOpenURL:theURL universalLink:nil];
}

/**
 handle open url
 */
- (BOOL)dl_handleOpenURL:(NSURL *)theURL universalLink:(NSUserActivity *)userActivity{
    [self dl_qqHandleOpenURL:theURL];
    
    if(userActivity){
        return [TencentOAuth HandleUniversalLink:theURL];
    }else{
        return [TencentOAuth HandleOpenURL:theURL];
    }
    return NO;
}

/**
 处理来至QQ的响应
 */
- (void)onResp:(QQBaseResp *)resp{
    if ([resp isKindOfClass:[SendMessageToQQResp class]]) {
        BOOL result = NO;
        if ([resp.result integerValue] == 0) {
            result = YES;
        }else{
            result = NO;
        }
        if([self.delegate respondsToSelector:@selector(dl_sharedMessageCallBack:responseData:)]){
            [self.delegate dl_sharedMessageCallBack:result responseData:resp];
        }
        
        ///分享回调带状态
        QQShareCallBackStatusType shareStatusType;
        if ([resp.result isEqualToString:@"0"]) {//分享成功
            shareStatusType = QQShareCallBackStatusTypeSuccess;
        }else if ([resp.result isEqualToString:@"-4"]) {//分享取消
            shareStatusType = QQShareCallBackStatusTypeCancel;
        }else{//分享失败
            shareStatusType = QQShareCallBackStatusTypeFail;
        }
        if ([self.delegate respondsToSelector:@selector(dl_sharedMessageCallBackStatusType:responseData:)]) {
            [self.delegate dl_sharedMessageCallBackStatusType:shareStatusType responseData:resp];
        }
        if (self.dl_shareMessageCallBack) {
            self.dl_shareMessageCallBack(shareStatusType, resp);
        }
    }
       
}

@end
